package com.personnel.util;

import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @Author: YuePeng
 * @Date: 2022/1/26 10:42
 * @Version: 1.0
 */
public class FileUtil {
    public static String fileUpLoad(MultipartFile file, String abPath, HttpServletRequest request) throws IOException {

        String uploadPath = "D:\\IdeaProject\\school\\personnel\\src\\main\\resources\\static\\upload\\"+abPath+"\\";
        //String uploadPath = "/home/springboot/upload/"+abPath + "/";

        System.out.println("uploadPath="+uploadPath);
        String fileName = UUID.randomUUID().toString().replace("-","") + file.getOriginalFilename();  // 获取上传的文件名
        File newFile = new File(uploadPath + fileName);         // 根据当前工程的物理路径+文件名生成一个file对象
        System.out.println("newFile=" + newFile);
        FileCopyUtils.copy(file.getBytes(), newFile);                   // 通过上传通道中的字节码完成file文件的写入操作
        String userSQLPath = File.separator + "upload"+ File.separator + abPath + File.separator + fileName;  // 组装一个浏览器识别的文件地址
        System.out.println("userSQLPath=" + userSQLPath);
        return userSQLPath;
    }
}
